import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;


public class evalb {
	
	private long matchcount;
	private long goldcount;
	private long parsecount;
	
	public void init()
	{
		matchcount=0;
		goldcount=0;
		parsecount=0;
	}
	public double fone(String output, String gold) throws IOException
	{
		List<Node> outtrees = toTrees(output);
		List<Node> goldtrees = toTrees(gold);
		if (outtrees.size()!=goldtrees.size())
		{
			System.out.println("size not match!");
			System.exit(-1);
		}
		for (int i=0; i<outtrees.size(); i++)
		{
			eval(outtrees.get(i),goldtrees.get(i));
		}
		System.out.println(output + " : " + parsecount + " brackets");
		System.out.println(gold + " : " + goldcount + " brackets");
		System.out.println("matching : " + matchcount + " brackets");
		System.out.println("precision : " + (matchcount + 0.0d)/parsecount);
		System.out.println("recall : " + (matchcount + 0.0d)/goldcount);
		System.out.println("F1 : " + 2.0d/(parsecount/(matchcount + 0.0d) + goldcount/(matchcount + 0.0d)));
		return 2.0d/(parsecount/(matchcount + 0.0d) + goldcount/(matchcount + 0.0d));
	}
	
	private void eval(Node out, Node gold) {
		HashMap<String, Long> outspan = new HashMap<String, Long>();
		HashMap<String, Long> goldspan = new HashMap<String, Long>();
		span(out, 0l, outspan);
		span(gold, 0l, goldspan);
		for (String key:outspan.keySet())
			parsecount = parsecount + outspan.get(key);
		for (String key: goldspan.keySet())
			goldcount = goldcount + goldspan.get(key);
		for (String key:outspan.keySet())
		{
			if (goldspan.containsKey(key))
				matchcount = matchcount + min(outspan.get(key), goldspan.get(key));
		}
	}
	private long min(long long1, long long2) {
		return long2<long1?long2:long1;
	}
	private long span(Node node, long index, HashMap<String, Long> rootspan) {
		long index0 = index;
		long j0;
		if (node.children!=null && node.children.size()>0)
		{
			for (Node child : node.children)
			{
				index = span(child, index, rootspan);
			}
			j0 = index;
		}
		else
		{
			j0 = index0 + 1;
		}
		String key = node.value + "+" + index0 + "+" + j0;
		if (rootspan.containsKey(key))
			rootspan.put(key, rootspan.get(key)+1);
		else
			rootspan.put(key, 1l);
		return j0;
	}
	public List<Node> toTrees(String file) throws IOException {
		BufferedReader br = getBr(file);
		String line;
		List<Node> roots = new LinkedList<Node>();
		while ((line=br.readLine())!=null)
			roots.add(toTree(line));
		br.close();
		return roots;
	}

	private BufferedReader getBr(String file) throws FileNotFoundException {
		return new BufferedReader(new FileReader(file));
	}
	
	private Node toTree(String line) {
		List<String> tokens = prepare(line);
		Node current = null;
		for (String s:tokens)
		{
			if (s.startsWith("("))
			{
				if (current==null)
				{
					current = new Node();
					current.parent=null;
					current.value=s.substring(1);
					current.children=new LinkedList<Node>();
				}
				else
				{
					Node temp = new Node();
					temp.parent = current;
					temp.value = s.substring(1);
					temp.children=new LinkedList<Node>();
					if (current.children==null)
						current.children=new LinkedList<Node>();
					current.children.add(temp);
					current = temp;
				}
			}
			else if (!s.equals(")"))
			{
				current.terminal=s;
			}
			else
			{
				if (current.parent!=null)
					current=current.parent;
			}
		}
		return current;
	}
	
	private List<String> prepare(String line) {
		String pline = line.trim();
		String[] tokens = pline.split(" ");
		System.out.println(line);
		List<String> result = new LinkedList<String> ();
		for (String s:tokens)
		{
			if (s.startsWith("("))
				result.add(s);
			else
			{
				int first = s.indexOf(")");
				result.add(s.substring(0, first));
				for (int i=first;i<s.length();i++)
					result.add(")");
			}
		}
		return result;
	}
	
	public static void main(String[] args) throws IOException
	{
		evalb evalb = new evalb();
		evalb.init();
		evalb.fone("test1", "test2");
	}
}
